const express = require('express');
const router = express.Router();
const loginSQL = require('../../db/loginSQL')
const noticeSQL = require('../../db/noticeSQL')

const bodyParser = require('body-parser');

// 这是一个删除公告的路由
router.post('/delnotice', bodyParser.json(), (req, res) => {
    // 检查公告ID是否为空
    if (req.body["postID"] == undefined
        || req.body["postID"] == null
        || typeof req.body["postID"] != "number") {
        res.json({
            err: 1,
            message: '公告ID为空'
        })
    } else if (!req.body["userID"]
        || typeof req.body["userID"] != "number") {
        res.json({
            err: 1,
            message: '管理员ID为空'
        })
    } else {
        var postID = req.body["postID"]
        var userID = req.body["userID"]
        loginSQL.getByID(userID, (result) => {
            var userPower = eval(result)[0].userPower
            var userStatus = eval(result)[0].userStatus
            if (userStatus == 0) {
                res.json({
                    err: 3,
                    message: '用户被封禁'
                })
            }
            else if (userPower == 0) {
                res.json({
                    err: 2,
                    message: '权限不足',
                    userPower: 0
                })
            }
            else {
                noticeSQL.delnotice(postID, () => {
                    res.json({
                        err: 200,
                        message: '删除公告成功'
                    })
                })
            }
        })
    }
})

module.exports = router;